﻿using System;
using Gonte.Types;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyUtilitiesTest
{
    /// <summary>
    ///This is a test class for DynamicClassTest and is intended
    ///to contain all DynamicClassTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DynamicClassTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod]
        public void DynamicClassCreateEmptyInstanceTest()
        {
            DynamicClass @class = DynamicClass.GetClass("TestClass");
            Assert.IsNotNull(@class);
            Assert.AreEqual("TestClass", @class.Name);

            DynamicObject @object = @class.CreateInstance();
            Assert.IsNotNull(@object);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void DynamicClassCreateInstanceNoFieldsTest()
        {
            DynamicClass @class = DynamicClass.GetClass("TestClass2");
            Assert.IsNotNull(@class);

            DynamicObject @object = @class.CreateInstance();
            Assert.IsNotNull(@object);

            @object.SetValue("Field1", 3);
            // It throws an exception because the field does not exist in the class
        }

        [TestMethod]
        public void DynamicClassSetFieldBeforeCreatingInstanceTest()
        {
            DynamicClass @class = DynamicClass.GetClass("TestClass3");
            Assert.IsNotNull(@class);

            @class.FieldsInfo.Add("Field1", new DynamicFieldInfo("Field1", 5));

            DynamicObject @object = @class.CreateInstance();
            Assert.IsNotNull(@object);

            Assert.AreEqual(5, @object.GetValue("Field1"));

            @object.SetValue("Field1", 3); // Overwrite the value
            Assert.AreEqual(3, @object.GetValue("Field1"));
        }

        [TestMethod]
        public void DynamicClassSetFieldAfterCreatingInstanceTest()
        {
            DynamicClass @class = DynamicClass.GetClass("TestClass");
            Assert.IsNotNull(@class);

            DynamicObject @object = @class.CreateInstance();
            Assert.IsNotNull(@object);

            // We modify the class after the object was created
            @class.FieldsInfo.Add("Field1", new DynamicFieldInfo("Field1", 5));

            Assert.AreEqual(5, @object.GetValue("Field1"));

            @object.SetValue("Field1", 3); // Overwrite the value
            Assert.AreEqual(3, @object.GetValue("Field1"));
        }
    }
}
